﻿namespace blackhouse.mojoPortalExtend
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Runtime.InteropServices;
    using System.Text.RegularExpressions;
    using System.Web;
    using System.Xml;

    public class SkinOverride : IHttpModule
    {
        private List<SkinOverrideItem> list_priv;

        private void context_BeginRequest(object sender, EventArgs e)
        {
            HttpApplication context = sender as HttpApplication;
            this.SetConfiguration(context);
            string url = context.Request.Url.ToString();
            SkinOverrideItem findToOverride = this.FindItem(url);
            if (!string.IsNullOrEmpty(findToOverride.Skin))
            {
                HttpContext.Current.Items.Add("skin", findToOverride.Skin);
            }
        }

        public void Dispose()
        {
        }

        private SkinOverrideItem FindItem(string url)
        {
            return this.list.FirstOrDefault<SkinOverrideItem>(f => f.Regex.IsMatch(url));
        }

        public void Init(HttpApplication context)
        {
            context.BeginRequest += new EventHandler(this.context_BeginRequest);
        }

        public void OnLogRequest(object source, EventArgs e)
        {
        }

        private void SetConfiguration(HttpApplication context)
        {
            if (this.list == null)
            {
                this.list = new List<SkinOverrideItem>();
                FileInfo file = new FileInfo(context.Server.MapPath("~/SkinOverride.config"));
                if (file.Exists)
                {
                    XmlDocument document = new XmlDocument();
                    document.Load(file.FullName);
                    XmlNodeList xmlList = document.SelectNodes("Overrides/Override");
                    foreach (XmlElement node in xmlList)
                    {
                        XmlNode xmlRegex = node.SelectSingleNode("RegexFind");
                        XmlNode xmlSkin = node.SelectSingleNode("NewSkin");
                        if ((xmlRegex != null) && (xmlSkin != null))
                        {
                            string regex = xmlRegex.InnerText;
                            string newSkin = xmlSkin.InnerText;
                            if (!string.IsNullOrEmpty(regex) && !string.IsNullOrEmpty(newSkin))
                            {
                                SkinOverrideItem item = new SkinOverrideItem {
                                    Regex = new Regex(regex),
                                    Skin = newSkin
                                };
                                this.list.Add(item);
                            }
                        }
                    }
                }
            }
        }

        private List<SkinOverrideItem> list
        {
            get
            {
                return this.list_priv;
            }
            set
            {
                this.list_priv = value;
            }
        }

        [StructLayout(LayoutKind.Sequential)]
        private struct SkinOverrideItem
        {
            public System.Text.RegularExpressions.Regex Regex;
            public string Skin;
        }
    }
}

